﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIGEB.Modelo.moduloTipoDocumentos;
using Npgsql;
using System.Data;

namespace SIGEB.Modelo.moduloDocumentos
{
    public class conjuntoValores:conjuntoBase
    {
        #region "constructores"

        public conjuntoValores(datosAplicacion dA)
            : base(dA)
        {
            this.numTipo = conjuntoBase.CONJVALORES;
        }

        #endregion

        #region "imec"

        public void agregarvalor(string idDoc, valor nuevoVal)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT registrar_valorCampo('" + nuevoVal.campoText + "', '" + nuevoVal.idAtributo + "', '" + idDoc + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al agregar valor");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            notificar(this.numTipo, null);
        }

        public void eliminarvalor(string idDoc, string idAtr)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT eliminar_valorCampo('" + idAtr + "', '" + idDoc + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "Error al eliminar valor");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
        }

        public void modificarvalor(String idDoc, String codAtr, valor nuevoVal)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT actualizar_valorCampo('" + nuevoVal.campoText + "', '" + codAtr + "', '" + idDoc + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch
            {
                throw new Exception("Error al modificar valor");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
        }

        public valor obtenervalor(string idDoc, string idAtr)
        {
            valor nuevo = null;
            try
            {
                compConexion.Open();
                string sql = "SELECT * from consultar_valorCampo_id('" + idAtr + "', '" + idDoc + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 3);
                    nuevo = new valor(valores[1], valores[0]);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el valor ");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            return nuevo;
        }

        public LinkedList<valor> obtenerTodos()
        {
            LinkedList<valor> valoresN = new LinkedList<valor>();
            try
            {
                compConexion.Open();
                string sql = "SELECT * from consultar_valorCampo()";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 3);
                    valoresN.AddLast(new valor(valores[1], valores[0]));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener todo los valores");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            return valoresN;
        }

        public LinkedList<valor> obtenerByDocId(string idDoc)
        {
            LinkedList<valor> valoresN = new LinkedList<valor>();
            try
            {
                compConexion.Open();
                string sql = "SELECT * from consultar_valorCampo_idDocumento('" + idDoc + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 3);
                    valoresN.AddLast(new valor(valores[1], valores[0]));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener todo los valores");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            return valoresN;
        }

        #endregion


        public override object obtenerEstado()
        {
            throw new NotImplementedException();
        }
    }
}
